package com.android.ccarvajal.ejemplos;

import java.util.List;
import java.util.Vector;
import android.app.Activity;
import android.app.ListActivity;
import android.content.Context;
import android.net.ParseException;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
//import android.view.ViewGroup.LayoutParams;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ImageView;
//import android.widget.GridView;
//import android.widget.ListView;
import android.widget.TextView;
//import android.widget.Toast;

public class GaleriaProductosGeneral extends ListActivity {
	/** Called when the activity is first created. */
	
	private Integer[] mThumbIds = {
            R.drawable.placasfibuladistal
            ,R.drawable.claviculaloc2
            ,R.drawable.placashumerophr2
            ,R.drawable.placasdmrloc
            ,R.drawable.placaslocdobleperfil
            ,R.drawable.femurcfl
            ,R.drawable.tibiaplgolfloc
            ,R.drawable.tibiaprlloc35
            ,R.drawable.tibiamdloc
            ,R.drawable.tibiaaldloc
            ,R.drawable.placatloc50mm
            ,R.drawable.placalloc50mm
            ,R.drawable.sdcsistemadinamicocadera
            ,R.drawable.sdcsistemadinamicocondilar
            ,R.drawable.femurrploc
    };
	
	
	static final String[] mTitlesIds = new String[] {
        "Placas de fibula distal"
        ,"Clavicula Loc II"
        ,"Placas Humero PHR III"
        ,"Placas DMR Loc"
        ,"Placas Loc Doble Perfil"
        ,"Femur CFL"
        ,"Tibia PL Golf LOC"
        ,"Tibia PRL Loc 3.5"
        ,"Tibia MD Loc"
        ,"Tibia ALD Loc"
        ,"Placa T Loc 5.0 mm"
        ,"Placa L Loc 5.0 mm"
        ,"SDC Sistema Dinamico de Cadera"
        ,"SDC Sistema Dinamico Condilar"
        ,"Femur RP Loc"
      };
	
	private LayoutInflater layoutx;
	private Vector<RowData> listValue;
	RowData rowData;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
    	super.onCreate(savedInstanceState);
    	setContentView(R.layout.galeriaproductos);
    	
    	layoutx = (LayoutInflater) getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
    	listValue = new Vector<RowData>();
    	for(int i=0;i<mThumbIds.length;i++){
    		try{
    		rowData = new RowData(mTitlesIds[i], "", i);
    		}catch (ParseException e) 
			{
			    e.printStackTrace();
			}
    		listValue.add(rowData);
    	}
    	
    	//GridView Categorias = (GridView)findViewById(R.id.gridcataloggeneral);
    	//Categorias.setAdapter(new AdaptadorImagenCatalogo(this));
    	
    	CustomAdapter AdaptadorPersonalizado = new CustomAdapter(this, R.layout.griditem, R.id.tvtituloimagenes, listValue);
    	setListAdapter(AdaptadorPersonalizado);
    	getListView().setTextFilterEnabled(true);
    	
    }
    
    protected class AdaptadorImagenCatalogo extends BaseAdapter {
    	
    	private Context mContext;
    	private LayoutInflater inflater;
    	
    	public AdaptadorImagenCatalogo(Context c){
    		mContext = c;
    	}

    	public int getCount() {
    		// TODO Auto-generated method stub
    		return mThumbIds.length;
    	}

    	public Object getItem(int position) {
    		// TODO Auto-generated method stub
    		return null;
    	}

    	public long getItemId(int position) {
    		// TODO Auto-generated method stub
    		return 0;
    	}

    	public View getView(int position, View convertView, ViewGroup parent) {
    		// TODO Auto-generated method stub
    		View resultview = null;
    		ImageView imageView;
    		TextView tituloImagen;
    		inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    		if (convertView == null) {  // if it's not recycled, initialize some attributes
    			resultview = inflater.inflate(R.layout.griditem, null);
    			imageView = (ImageView) resultview.findViewById(R.id.imagegrid);
    			imageView.setImageResource(mThumbIds[position]);
    			//imageView.setLayoutParams(new GridView.LayoutParams(LayoutParams.MATCH_PARENT, 400));
                //imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
                //imageView.setPadding(2, 2, 2, 2);
    			
    			tituloImagen = (TextView) resultview.findViewById(R.id.tvtituloimagenes);
    			tituloImagen.setText(mTitlesIds[position].toUpperCase());
            } else {
                //imageView = (ImageView) convertView;
            	resultview = convertView;
            }

            //imageView.setImageResource(mThumbIds[position]);
            return resultview;
    	}
    	
    }
    
    private class CustomAdapter extends ArrayAdapter<RowData>{

		public CustomAdapter(Context context, int resource, int textViewResourceId, List<RowData> objects) {
			super(context, resource, textViewResourceId, objects);
			// TODO Auto-generated constructor stub
		}
		
		public View getView(int position, View convertView, ViewGroup parent)
		{
			ContenedorVista contenedor = null;
			TextView titulo = null;
			//TextView detalles = null;
			ImageView imagen = null;
			RowData DatosFila = getItem(position);
			if(null==convertView)
			{
				convertView = layoutx.inflate(R.layout.griditem, null);
				contenedor = new ContenedorVista(convertView);
				convertView.setTag(contenedor);
			}
			
			contenedor = (ContenedorVista) convertView.getTag();
			imagen = contenedor.obtenerImagen();
			imagen.setImageResource(mThumbIds[DatosFila.mId]);
			titulo = contenedor.obtenerTitulo();
			titulo.setText(DatosFila.mNames.toUpperCase());
			//detalles = contenedor.obtenerDetalles();
			//detalles.setText(DatosFila.mNames);
			
			return convertView;
		}
		
		private class ContenedorVista
		{
			private View mFila;
			private TextView titulo=null;
			//private TextView detalles=null;
			private ImageView imagen=null;
			
			public ContenedorVista(View fila)
			{
				mFila=fila;
			}
			
			public TextView obtenerTitulo()
			{
				if(null==titulo)
				{
					titulo = (TextView)mFila.findViewById(R.id.tvtituloimagenes);
				}
				return titulo;
			}
			
			/*public TextView obtenerDetalles()
			{
				if(null==detalles)
				{
					detalles=(TextView)mFila.findViewById(R.id.tvtituloimagenes);
				}
				return detalles;
			}*/
			
			public ImageView obtenerImagen()
			{
				if(null==imagen)
				{
					imagen = (ImageView)mFila.findViewById(R.id.imagegrid);
				}
				return imagen;
			}
			
		}
    	
    }
    
    
    private class RowData
    {
    	
    	protected String mNames;
		protected String mUsn;
		protected int mId;
		
		public RowData(String title,String detail,int id)
		{
			mId=id;
			mNames = title;
			mUsn = detail;
		}

		@Override
		public String toString()
		{
	       return mNames + " " + mUsn + " " + mId;
		}
    }
}
